{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ch05 选择与循环"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 布尔值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(1<2<3)\n",
    "\n",
    "print(1<2>3)\n",
    "\n",
    "print(1<3>2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1, 2, 3) < (1, 2, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[1, 2, 3] < [1, 2, 4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'ABC' < 'C' < 'Pascal' < 'Python'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1, 2, 3, 4) < (1, 2, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1, 2) < (1, 2, -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1, 2, 3) == (1.0, 2.0, 3.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1) 选择结构\n",
    "#### 例1：录用资格确认"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "恭喜，你已获得我公司的录用机会!\n"
     ]
    }
   ],
   "source": [
    "age=24\n",
    "subject=\"计算机\"\n",
    "college=\"非重点\"\n",
    "if (age > 25 and subject==\"电子信息工程\") or (college==\"重点\" and subject==\"电子信息工程\" ) or (age<=28 and subject==\"计算机\"):\n",
    "    print(\"恭喜，你已获得我公司的录用机会!\")\n",
    "else:\n",
    "    print(\"抱歉，你未达到录用要求\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `types`模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个正整数: 12\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "#例2：选择结构应用\n",
    "#例2：用户输入若干个分数，求所有分数的平均分。每输入一个数后询问是否继续输入下一个数，答“yes”就继续输入下一个分数，回答“no”就停止输入分数\n",
    "import types\n",
    "endFlag='yes'\n",
    "s = 0\n",
    "while endFlag.lower() == 'yes':\n",
    "    x = input(\"请输入一个正整数: \")\n",
    "    print(type(x))\n",
    "#     x = eval(x)\n",
    "    x = int(x)\n",
    "    if isinstance(x,int) and 0 <= x <= 100:\n",
    "        s = s + x\n",
    "    else:\n",
    "        print('不是数字')\n",
    "    endFlag=input('继续输入？(yes or no)')\n",
    "    \n",
    "print('整数之和=', s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "eval?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 永不结束的死循环,Ctrl+C强行终止"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\ni=0\\nwhile i<10:\\n    if i%2==0:\\n        continue\\n    print(i)\\n    i+=1\\n'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "i=0\n",
    "while i<10:\n",
    "    if i%2==0:\n",
    "        continue\n",
    "    print(i)\n",
    "    i+=1\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    if i%2==0:\n",
    "        continue\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    if i%2==0:\n",
    "        i+=1 \n",
    "        continue\n",
    "    print(i),"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "140069521312160 : 0\n",
      "140069521312192 : 1\n",
      "140069521312224 : 2\n",
      "140069521312256 : 3\n",
      "140069521312288 : 4\n",
      "140069521312320 : 5\n",
      "140069521312352 : 6\n"
     ]
    }
   ],
   "source": [
    "\n",
    "for i in range(7):    \n",
    "    print(id(i),':',i)   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2) 循环结构\n",
    "### 例子3: 计算`1+2+3+…+100` 的值。\n",
    "### 计算`1+2+3+…+100` 的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1+2+3+…+100 =  5050\n"
     ]
    }
   ],
   "source": [
    "s=0\n",
    "for i in range(1, 101):\n",
    "    s = s + i\n",
    "\n",
    "print('1+2+3+…+100 = ', s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1+2+3+…+100 =  5050\n"
     ]
    }
   ],
   "source": [
    "print('1+2+3+…+100 = ', sum(range(1,101)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### enumerate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# enumerate(iterable[, start]) -> iterator for index, value of iterable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表的第 1 个元素是： a\n",
      "列表的第 2 个元素是： b\n",
      "列表的第 3 个元素是： mpilgrim\n",
      "列表的第 4 个元素是： z\n",
      "列表的第 5 个元素是： example\n"
     ]
    }
   ],
   "source": [
    "#输出序列中的元素\n",
    "a_list=['a', 'b', 'mpilgrim', 'z', 'example']\n",
    "for i,v in enumerate(a_list):\n",
    "    print('列表的第', i+1, '个元素是：', v)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n",
      "14\n",
      "21\n",
      "28\n",
      "42\n",
      "49\n",
      "56\n",
      "63\n",
      "77\n",
      "84\n",
      "91\n",
      "98\n"
     ]
    }
   ],
   "source": [
    "#求1~100之间能被7整除\n",
    "for i in range(1,101):\n",
    "    if i % 7 == 0 and i % 5 != 0:\n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "153\n",
      "370\n",
      "371\n",
      "407\n"
     ]
    }
   ],
   "source": [
    "#水仙花数\n",
    "for i in range(100,1000):\n",
    "    ge =  i % 10\n",
    "    shi = i // 10 % 10\n",
    "    bai = i // 100\n",
    "    if ge**3+shi**3+bai**3 == i:\n",
    "        print(i)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "82.375\n",
      "82.375\n"
     ]
    }
   ],
   "source": [
    "#求平均分\n",
    "score = [70, 90, 78, 85, 97, 94, 65, 80]\n",
    "s = 0\n",
    "for i in score:\n",
    "    s += i\n",
    "\n",
    "print(s*1.0/len(score))\n",
    "print(sum(score)*1.0/len(score))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 打印九九乘法表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 * 1 = 1 \t\n",
      "\n",
      "2 * 1 = 2 \t2 * 2 = 4 \t\n",
      "\n",
      "3 * 1 = 3 \t3 * 2 = 6 \t3 * 3 = 9 \t\n",
      "\n",
      "4 * 1 = 4 \t4 * 2 = 8 \t4 * 3 = 12 \t4 * 4 = 16 \t\n",
      "\n",
      "5 * 1 = 5 \t5 * 2 = 10 \t5 * 3 = 15 \t5 * 4 = 20 \t5 * 5 = 25 \t\n",
      "\n",
      "6 * 1 = 6 \t6 * 2 = 12 \t6 * 3 = 18 \t6 * 4 = 24 \t6 * 5 = 30 \t6 * 6 = 36 \t\n",
      "\n",
      "7 * 1 = 7 \t7 * 2 = 14 \t7 * 3 = 21 \t7 * 4 = 28 \t7 * 5 = 35 \t7 * 6 = 42 \t7 * 7 = 49 \t\n",
      "\n",
      "8 * 1 = 8 \t8 * 2 = 16 \t8 * 3 = 24 \t8 * 4 = 32 \t8 * 5 = 40 \t8 * 6 = 48 \t8 * 7 = 56 \t8 * 8 = 64 \t\n",
      "\n",
      "9 * 1 = 9 \t9 * 2 = 18 \t9 * 3 = 27 \t9 * 4 = 36 \t9 * 5 = 45 \t9 * 6 = 54 \t9 * 7 = 63 \t9 * 8 = 72 \t9 * 9 = 81 \t\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10):\n",
    "    for j in range(1,i+1):\n",
    "        print(i,'*',j,'=',i*j,'\\t',end='')\n",
    "    print('\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "187\n"
     ]
    }
   ],
   "source": [
    "#200以内能被17整除的最大正整数\n",
    "for i in range(200,0,-1):\n",
    "    if i%17 == 0:\n",
    "        print(i)\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "187\n"
     ]
    }
   ],
   "source": [
    "a = 17\n",
    "for i in range(0,200,17):\n",
    "    if i % 17 == 0:\n",
    "        a = i\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Input a integer:17\n",
      "Yes\n"
     ]
    }
   ],
   "source": [
    "#判断一个数是否为素数\n",
    "import math\n",
    "n = input(\"Input a integer:\")\n",
    "n=int(n)\n",
    "m =math.ceil(math.sqrt(n) + 1)\n",
    "\n",
    "for i in range(2,m):\n",
    "    if n % i == 0 and i < n:\n",
    "        print('No')\n",
    "        break\n",
    "else:\n",
    "    print('Yes')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
